<template>
  <div class="login-container">
    <van-nav-bar title="登录" class="page-nav-bar">
      <template #left>
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <!-- 表单验证 -->
      <!-- 给 van-field 组件配置验证规则-->
      <!-- 当表单提交的时候会自动触发表单验证 -->
      <!-- 如果验证通过会触发submit事件,不通过则不会触发 -->
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model.number="user.mobile"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        v-model="user.code"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="1000 * 60"
            v-if="iscountShow"
            @finish="iscountShow = false"
            format="ss秒后重新发送"
          />
          <van-button
            size="small"
            round
            type="default"
            class="send-sms-btn"
            @click="onSendSms"
            native-type="button"
            v-else
          >
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit" color="#6db4fb">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data() {
    return {
      // 用户输入的信息
      user: {
        mobile: '',
        code: ''
      },
      // 表单验证规则
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      // 是否展示倒计时
      iscountShow: false
    }
  },
  methods: {
    // 登录
    async onSubmit() {
      // 1.获取表单数据
      const user = this.user
      // 2.表单验证
      this.$toast.loading({
        message: '登录中...', // 提示框,提示信息
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间,默认两秒,如果为0,则持续展示
      })
      // 3. 提交请求登录
      try {
        const { data: res } = await this.$api.login(user)
        console.log(res)
        this.$store.commit('setUser', res.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (err) {
        // 4.根据响应结果后续操作
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          // console.log('登录失败,请稍后重试', error)
          this.$toast.fail('登录失败,请稍后重试')
        }
      }
    },
    // 发送验证码
    async onSendSms() {
      this.$toast.loading({
        message: '发送中...', // 提示框,提示信息
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间,默认两秒,如果为0,则持续展示
      })
      // 1.校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 2.验证通过,显示倒计时
      this.iscountShow = true
      // 3.请求发送验证码
      try {
        await this.$api.sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败关闭倒计时
        this.iscountShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了,请稍后重试')
        } else {
          this.$toast('发送失败,请稍后重试')
        }
        console.log(err)
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 38px;
  }
  .send-sms-btn {
    width: 156px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
  }
}
</style>
